use axum::{
    http::StatusCode, 
    serve, 
    Json, 
    Router, 
    routing,
    extract::Path
};

async fn healthcheck() -> (StatusCode, Json<String>) {
    (StatusCode::OK, Json("OK".to_string()))
}

async fn greet(name: Option<Path<String>>) -> (StatusCode, Json<String>) {
    match name{
        Some(name) => (StatusCode::OK, Json(format!("Hello, {}!", name.as_str()))),
        None => (StatusCode::OK, Json("Hello, World!".to_string())),
    }

}

#[tokio::main]
async fn main(){
    let app = Router::new()
        .route("/healthcheck", routing::get(healthcheck))
        .route("/greet/:name", routing::get(greet))
        .route("/greet", routing::get(greet));
    let lisenter = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();

    serve(lisenter, app).await.unwrap();

}
